<?php use_stylesheets_for_form($form) ?>
<?php use_javascripts_for_form($form) ?>

<script type="text/javascript">
<!--
$(function(){
	$("#product_clientcharge_name").click(function(){
		var client = $("#product_client").val();
		$.ajax({
			   type: "POST",
			   url: "<?php echo url_for('product/clientChargeList');?>",
			   data: "client="+client,
			   success: function(string){
			       var clientcharges = string.split(",");
			       $("#product_clientcharge_name").autocomplete({
				      	source : clientcharges
				    });
			   },
			   error: function(){
				   alert("error");
			   }
		});

	});

	$("#product_agentcharge_name").click(function(){
		var agent = $("#product_agent").val();
		$.ajax({
			   type: "POST",
			   url: "<?php echo url_for('product/agentChargeList');?>",
			   data: "agent="+agent,
			   success: function(string){
			       var agentcharges = string.split(",");
			       $("#product_agentcharge_name").autocomplete({
				      	source : agentcharges
				    });
			   },
			   error: function(){
				   alert("error");
			   }
		});

	});

});
	var errorMessage="";
	function checkEventValid(){
	    var client = $('#product_client').val();
	    var blogger = $('#product_blogger_id').val();
	    var startdate = $('#product_startdate').val();
	    var enddate = $('#product_enddate').val();
	    var isvalid = false;
		$.ajax({
			   type: "POST",
			   url: "<?php echo url_for('product/checkValid');?>",
			   data: "client="+client+"&blogger="+blogger+"&startdate="+startdate+"&enddate="+enddate,
			   success: function(string){
			       alert( "Data Saved: " + string );
			   	   return true;
			   },
			   error: function(){
				   alert("error");
				   return false;
			   }
		});
		return true;
	}
	function validate(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   if(reg.test(email) == false) {
	      return false;
	   }
	   return true;
	}
	function emailsValidator(string){
		var emails = string.split(",");
		for ( var index = 0; index < emails.length; index++) {
			if(!validate(emails[index])){
				errorMessage = "error is at "+emails[index];
				return false;
			}
		}
		return true;
	}
	function checkValidator(){
		var isValid = true;
		
		//check client charge email valid
		/**
		var clientcharge_email = $("#product_clientcharge_email").val();
		if(clientcharge_email == null || clientcharge_email ==""){
			$(".c_eml_error").html("email required");
			isValid = false;
		}
		else if(!emailsValidator(clientcharge_email)){
			var temp = errorMessage;
			errorMessage = "clientcharge email "+temp;
			$(".c_eml_error").html(errorMessage);
			isValid = false;
		}
		else{
		    $(".c_eml_error").html("");
		}

		//check agent charge email valid
		var agentcharge_email = $("#product_agentcharge_email").val();
		if(agentcharge_email == null || agentcharge_email ==""){
			$(".a_eml_error").html("email required");
			isValid = false;
		}
		else if(!emailsValidator(agentcharge_email)){
			var temp = errorMessage;
			errorMessage = "alientcharge email "+temp;
			$(".a_eml_error").html(errorMessage);
			isValid = false;
		}
		else{
			$(".a_eml_error").html("");
		}
		*/
		//check sale and code valid 
		var sale = parseInt($("#product_sale").val());
		var cost = parseInt($("#product_cost").val());
		if(sale < cost){
			$(".error").html("error sale < code");
			$(".sale th").css("background","#f3a6a6");
			$(".cost th").css("background","#f3a6a6");
			isValid = false;
		}
		return isValid;
	}
//-->
</script>
<div class="error">
<?php if(isset($error)) echo $error?>
</div>
<form id="product_form"
	action="<?php echo url_for('product/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>"
	method="post"
	<?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>
	onsubmit="return checkValidator();">
	<?php if (!$form->getObject()->isNew()): ?>
	<input type="hidden" name="sf_method" value="put" />
	<?php endif; ?>
	<table class="order_form">
		<tfoot>
			<tr>
				<td colspan="2"><?php echo $form->renderHiddenFields(false) ?>
					&nbsp;<a href="<?php echo url_for('product/index') ?>">戻る</a> <?php if (!$form->getObject()->isNew()): ?>
					&nbsp;<?php echo link_to('Delete', 'product/delete?id='.$form->getObject()->getId(), array('method' => 'delete', 'confirm' => 'Are you sure?')) ?>
					<?php endif; ?> <input id="submit" type="submit" value="保存" />
				</td>
			</tr>
		</tfoot>
		<tbody>
		<?php echo $form->renderGlobalErrors() ?>
			<tr class="client">

				<th><?php echo $form['client']->renderLabel() ?></th>
				<td><?php echo $form['client']->renderError() ?> <?php echo $form['client'] ?>
					<a href="<?php echo url_for("client/new")?>">New</a>
				</td>
			</tr>
			<tr class="clientcharge_name">

				<th><?php echo $form['clientcharge_name']->renderLabel() ?></th>
				<td><?php echo $form['clientcharge_name']->renderError() ?> <?php echo $form['clientcharge_name'] ?>
					<span>様</span>
				</td>
			</tr>


			<tr class="blogger_id">
				<th><?php echo $form['blogger_id']->renderLabel() ?></th>
				<td><?php echo $form['blogger_id']->renderError() ?> <?php echo $form['blogger_id'] ?>
					<a href="<?php echo url_for("blogger/new")?>">New</a>
				</td>
			</tr>

			<tr class="product">
				<th><?php echo $form['product']->renderLabel() ?></th>
				<td><?php echo $form['product']->renderError() ?> <?php echo $form['product'] ?>
				</td>
			</tr>
			<tr class="url">
				<th><?php echo $form['url']->renderLabel() ?></th>
				<td><?php echo $form['url']->renderError() ?> <?php echo $form['url'] ?>
				</td>
			</tr>
			<tr class="date">
				<th><?php echo $form['date']->renderLabel() ?></th>
				<td><?php echo $form['date']->renderError() ?> <?php echo $form['date'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_date'
						});

				</script>
				</td>
			</tr>
			<tr class="time">
				<th></th>
				<td><?php echo $form['starttime']->renderError() ?> <?php echo $form['starttime'] ?><span
					style="color: red"> ~ </span> <?php echo $form['endtime']->renderError() ?>
					<?php echo $form['endtime'] ?></td>
			</tr>

			<tr class="pledgetime">
				<th><?php echo $form['pledgetime']->renderLabel() ?></th>
				<td><?php echo $form['pledgetime']->renderError() ?> <?php echo $form['pledgetime'] ?>
				</td>
			</tr>
			<tr class="sale">
				<th><?php echo $form['sale']->renderLabel() ?></th>
				<td><?php echo $form['sale']->renderError() ?> <?php echo $form['sale'] ?>
				</td>
			</tr>
			<tr class="client_request_date">
				<th><?php echo $form['client_request_date']->renderLabel() ?></th>
				<td><?php echo $form['client_request_date']->renderError() ?> <?php echo $form['client_request_date'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_client_request_date'
						});

				</script>
				</td>
			</tr>
			<tr class="client_note">
				<th><?php echo $form['client_note']->renderLabel() ?></th>
				<td><?php echo $form['client_note']->renderError() ?> <?php echo $form['client_note'] ?>
				</td>
			</tr>
			<tr class="client_payment_time">
				<th><?php echo $form['client_payment_time']->renderLabel() ?></th>
				<td><?php echo $form['client_payment_time']->renderError() ?> <?php echo $form['client_payment_time'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_client_payment_time'
						});

				</script>
				</td>
			</tr>

			<tr class="agent">
				<th><?php echo $form['agent']->renderLabel() ?></th>
				<td><?php echo $form['agent']->renderError() ?> <?php echo $form['agent'] ?>
					<a href="<?php echo url_for("agent/new")?>">New</a>
				</td>
			</tr>
			<tr class="agentcharge_name">
				<th><?php echo $form['agentcharge_name']->renderLabel() ?></th>
				<td><?php echo $form['agentcharge_name']->renderError() ?> <?php echo $form['agentcharge_name'] ?>
					<span>様</span>
				</td>
			</tr>
	
			<tr class="cost">
				<th><?php echo $form['cost']->renderLabel() ?></th>
				<td><?php echo $form['cost']->renderError() ?> <?php echo $form['cost']?>
				</td>
			</tr>
			<tr class="agent_request_date">
				<th><?php echo $form['agent_request_date']->renderLabel() ?></th>
				<td><?php echo $form['agent_request_date']->renderError() ?> <?php echo $form['agent_request_date'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_agent_request_date'
						});

				</script>
				</td>
			</tr>
			<tr class="agent_note">
				<th><?php echo $form['agent_note']->renderLabel() ?></th>
				<td><?php echo $form['agent_note']->renderError() ?> <?php echo $form['agent_note'] ?>
				</td>
			</tr>
			<tr class="agent_payment_time">
				<th><?php echo $form['agent_payment_time']->renderLabel() ?></th>
				<td><?php echo $form['agent_payment_time']->renderError() ?> <?php echo $form['agent_payment_time'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_agent_payment_time'
						});

				</script>
				</td>

			</tr>
			<tr class="deadline">
				<th><?php echo $form['deadline_date']->renderLabel() ?></th>
				<td><?php echo $form['deadline_date']->renderError() ?> <?php echo $form['deadline_date'] ?>
					<script language="JavaScript">
						new tcal ({
							// form name
							'formname': 'product_form',
							// input name
							'controlname': 'product_deadline_date'
						});

				</script> &nbsp;&nbsp;&nbsp;&nbsp; <?php echo $form['deadline_time']->renderError() ?>
				<?php echo $form['deadline_time'] ?>
				</td>
			</tr>

			<tr class="user_gmail_acount">
				<th><?php echo $form['user_gmail_acount']->renderLabel() ?></th>
				<td><?php echo $form['user_gmail_acount']->renderError() ?> <?php echo $form['user_gmail_acount'] ?>
				</td>
			</tr>
			<tr class="user_gmail_password">
				<th><?php echo $form['user_gmail_password']->renderLabel() ?></th>
				<td><?php echo $form['user_gmail_password']->renderError() ?> <?php echo $form['user_gmail_password'] ?>
				</td>
			</tr>

			<tr>
			</tr>
		</tbody>
	</table>
</form>
